package com.atguigu.binaryTree;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class PreOrderInStack {
    public static List<Integer> preorderTraversal(TreeNode root){
        if(root==null) return null;
        Stack<TreeNode> stack=new Stack<>();
        List<Integer> list=new ArrayList<>();
        stack.push(root);
        while(!stack.isEmpty()){
            if(stack.peek()!=null){
                TreeNode tmp=stack.pop();
                list.add(tmp.val);
                if(tmp.right!=null)
                stack.push(tmp.right);
                if(tmp.left!=null)
                stack.push(tmp.left);
            }
        }
        return list;
    }
}
